home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: What is &Variable (declared as: char Variable[10])?
- Date: 25 Feb 1996 17:16:13 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4gr1ktINNqin@keats.ugrad.cs.ubc.ca>
- References: <4gqpa1$3h9@alcor.usc.edu>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4gqpa1$3h9@alcor.usc.edu>, Abu Wawda <wawda@alcor.usc.edu> wrote:
- >I'm having trouble understanding what the address of a static array
- >is.
-
- Read K&R, twice if you have to.
-
- >For example, if I declare a variable called myarray as:
- > char myarray[10];
- >then what could &myarray possibly mean? myarray is not a pointer, so
-
- No, myrray is not a pointer. But the _expression_ myarray yields a pointer to
- the first element of the array object myarray. Thus, a statement like this is
- legal:
-
- char *ptr = myarray; /* provided myarray is decl'd in scope */
-
- >&myarray could not possibly be the address of the variable myarray
- >(like it would be if I did char* myarray and then asked for &myarray).
-
- Yes, &myarray is the address of the variable myarray. It is a pointer, whose
- type is ``pointer to array of ten characters'', or, if you will: char (*)[10].
-
- >Functions such as scanf() allow the following:
- >
- > char myarray[10];
- >
- > scanf("%s",&myarray);
-
- This wrong. The expression &myarray is a pointer to an array of ten characters,
- as I just mentioned. Scanf requires the %s conversion parameter to be matched
- by a string, which is pointer to a character, not to an array of ten
- characters. Also, scanf() will overrun your array if the user (or input stream)
- causes more than 9 characters of input to be interpreted as the string. Use
- "%9s".
-
- Don't be fooled by the fact that the above works. It just so happens that
- myarray and &myarray are both expressions that yield the same pointer. But the
- two pointers have a different type. The compiler will not warn you of this
- error unless it is very sophisticated in that it can analyze scanf() format
- strings and match them against argument types.
-
- >but I don't understand what scanf() could possibly be taking in the
- >second parameter. It can't be: char** since myarray is not a
- >pointer.
-
- No, what you are giving to scanf is a char(*)[10]. Note the parentheses around
- *. Without them, you have char *[10], which is an array of ten character
- pointers, not a pointer to an array of ten characters.
-
- I CAN understand how the following would work:
- >
- > char* myarray;
- >
- > myarray = (char*) malloc(10);
- > scanf("%s",&myarray);
-
- This is extremely wrong, and will yield undefined results.
-
- >Then scanf() is simply taking a char** (pointer to a character
- >pointer, or in other words the address of the pointer myarray) in its
- >second paremeter. However, if I write my own function like this:
-
- scanf() does not manufacture string pointers. scanf does _not_ dynamically
- allocate strings. You have to provide the storage, and you pass a simple
- character pointer. The above is correct up to the malloc. But the scanf() call
- should be:
-
- scanf("%9s",myarray);
-
- By the way, you should not call it myarray in this case. The name of the
- variable is misleading. It is not an array, but a character pointer, which
- points to a dynamically allocated array. If you take its address, &myarray, you
- have a genuine char **, which is not a suitable argument to scanf() by any
- means.
-
- It just so happens that an array of type T, and a pointer to type T, can both
- be used in array expressions using the [] postfix operator, but a pointer
- variable is not an array object.
-
- > void func(char** p)
- > {
- > // do something here
-
- That is not a standard C comment. Avoid C++ comments in standard C programming.
-
- > }
- >
- >I cannot pass &myarray if I declare it as: char myarray[10]. So how do
- >this work? Thanks in advance,
-
- It cannot work that way. You want to modify the address of the array. However,
- the address of an array is not modifiable. The above routine can only be used
- with character pointers. If I have a character pointer, char *p, I can
- send its address to the function by doing func(&p), and func() is free to
- modify the value of p (by assigning to *p). However, this cannot be done with
- arrays. Arrays are not pointer variables, but names of storage which collapse
- into addresses when they are used in expressions.
-
- The only kind of array you could pass to func() would be a string vector:
-
- char *svec[] = { "Alpha", "Betta", "Gamma", 0 };
-
- The expression "svec" collapses into a pointer to the first element. Since the
- first element is a string (char *), the type of the expression svec is
- a char **. If you pass this to func, again, func() can only modify the
- character pointer referenced by svec---in other words, the character pointer
- svec[0], svec[1] and so forth. Svec itself can never be modified. So you have a
- choice. Pass to func() either a pointer to a single character pointer, or a
- pointer to a whole _array_ of these things.
-
- If you are expecting an array, you may wish to declare the function as
-
- void func(char *a[])
-
- to make this clear.
-
- However, this is exactly the same as writing char **a. In parameter
- declarations, all arrays silently turn into pointers. This is another fact of
- C, which complements the way in which array names turn into pointers inside
- expressions.
-
-
- --
-
-